package com.sun.jdk.fun;

import java.util.function.Function;

public class Demo01Fun {

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4};
        // 原始调用方式
        int result0 = fun1(arr,new Operator() {
            @Override
            public int getSum(int[] arr) {
                int sum = 0;
                for (int i : arr) {
                    sum += i;
                }
                return sum;
            }
        });
        System.out.println("result0="+result0);

        // lambda的调用方式
        int result1 = fun1(arr,(arr1) -> {
            int sum = 0;
            for (int i : arr) {
                sum += i;
            }
            return sum;
        });
        System.out.println("result1="+result1);

        int result2 = fun2(arr, (arr1) -> {
            int sum = 0;
            for (int i : arr) {
                sum += i;
            }
            return sum;
        });
        System.out.println("result2="+result2);
    }
    
    public static int fun1(int[] arr,Operator operator) {
        int sum = operator.getSum(arr);
        return sum;
    }
    
    // 调用JDK8的函数接口
    public static int fun2(int[] arr, Function<int[], Integer> function){
        Integer result = function.apply(arr);
        return result;
    }
}

/**
 * 函数式接口
 */
@FunctionalInterface
interface Operator {
    int getSum(int[] arr);
}
